import { IEventHandler, IRenderMessage } from '../EventHandler'

import fs from 'fs'
import path from 'path'

/**
 * 读取ts模板
 */
export class TsTemplateReader implements IEventHandler {
    async handle(messageId: string, params?: string): IRenderMessage<unknown> {
        const exePath = path.join(path.dirname(process.execPath), 'template', params + '.ts')
        if (path.fileExists(exePath)) {
            const data = await fs.readFile(exePath, 'utf8')
            try {
                const resultData = eval(data)
                return { messageId: messageId, data: resultData, isSuccess: true }
            } catch (error) {
                return { messageId: messageId, data: error.message, isSuccess: false }
            }
        }
        return { messageId: messageId, data: 'Can not find template', isSuccess: false }
    }
}
